package com.trkj.controllers;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.trkj.pojos.MedcineCompany;
import com.trkj.pojos.MedcineDetail;
import com.trkj.pojos.MedcineType;
import com.trkj.service.MedcineTypeService;

/**
 * 药库管理
 * 
 * @author Administrator
 *
 */
@Controller
public class MedcineStorageController {
	@Autowired
	private MedcineTypeService medcineTypeService;

	// 药品信息
	@GetMapping("/medcineDetailManage")
	public String medcineDetailManage() {
		return "medcineDetailManage";
	}
	
	/**
	 * 新增药品类
	 * @param medcineTypeName
	 * @param medcineTypeEffect
	 * @param medcineTypeNote
	 */
	@PostMapping("/addMedcineType")
	public void addMedcineType(String medcineTypeName,String medcineTypeEffect,String medcineTypeNote) {
		medcineTypeService.addMedcineType(medcineTypeName, medcineTypeEffect, medcineTypeNote);
	}
	
	/**
	 * 新增药品单位
	 * @param medcineCompanyName
	 */
	@PostMapping("/addMedcineCompany")
	public void addMedcineCompany(String medcineCompanyName) {
		medcineTypeService.addMedcineCompany(medcineCompanyName);
	}
	
	// 获取所有的药品类别
	@GetMapping("/getAllMedcineType")
	@ResponseBody
	public List<MedcineType> getAllMedcineType() {
		return medcineTypeService.getAllMedcineType();
	}
	//获取所有的药品类别封装成map
	@GetMapping("/getAllMedcineTypeMap")
	@ResponseBody
	public Map getAllMedcineTypeMap(int page,int rows) {
		return medcineTypeService.getAllmedcineTypeMap(page, rows);
	}

	// 获取所有的药品单位
	@GetMapping("/getAllMedcineCompany")
	@ResponseBody
	public List<MedcineCompany> getAllMedcineCompany() {
		return medcineTypeService.getAllMedcineCompany();
	}
	
	//获取到所有的药品单位封装成map
	@GetMapping("/getAllMedcineCompanyMap")
	@ResponseBody
	public Map<String,Object> getAllMedcineCompanyMap(int page,int rows){
		return medcineTypeService.getAllmedcineCommpanyMap(page, rows);
	}
	/**
	 * 修改药品详情
	 * @param medcineName
	 * @param medcineType
	 * @param medcinePrice
	 * @param medcineCompany
	 * @param medcineBaoZhiQi
	 * @param medcineNote
	 * @param medcineEffect
	 * @param medcineImage
	 */
	@RequestMapping("/editMedcineDetail")
	@ResponseBody
	public String editMedcineDetail(Integer medcineId,String medcineName, Integer medcineType, Double medcinePrice, Integer medcineCompany,Integer medcineBaoZhiQi,String medcineNote,String medcineEffect,String medcineImage) {
		System.out.println("medcineId=="+medcineId);
		medcineTypeService.editMedcineDetail(medcineId, medcineName, medcineType, medcinePrice, medcineCompany, medcineBaoZhiQi, medcineNote, medcineEffect, medcineImage);
		return "";
	}
	
	// 获取药品详情
	@GetMapping("/getAllMedcineDetails")
	@ResponseBody
	public Map getAllMedcineDetails(int page, int rows) {
		return medcineTypeService.getAllMedcineDetail(page, rows);
	}
	
	/**
	 * 删除一个药品详情
	 * @param medcineId
	 * @return
	 */
	@GetMapping("/deleteMedcineDetail")
	@ResponseBody
	public String deleteMedcineDetail(Long medcineId) {
		medcineTypeService.deleteMedcineDetail(medcineId);
		return "";
	}
	
	/**
	 * 按条件查询药品详情
	 * @param medcineTypeId
	 * @param medcineCompanyId
	 * @return
	 */
	@GetMapping("/searchSomeMedcineDetail")
	@ResponseBody
	public Map searchSomeMedcineDetail(int page,int rows,Long medcineTypeId,Long medcineCompanyId) {
		Map map = new HashMap<>();
		map.put("medcineTypeId", medcineTypeId);
		map.put("medcineCompanyId", medcineCompanyId);
		return medcineTypeService.searchSomeMedcineDetail(page,rows,map);
	}

	/**
	 * 新增药品
	 * @param medcineName
	 * @param medcineType
	 * @param medcinePrice
	 * @param medcineCompany
	 * @param medcineBaoZhiQi
	 * @param medcineNote
	 * @param medcineEffect
	 * @param medcineImage
	 */
	@PostMapping("/addMedcineDetail")
	public void addMedcineDetail(String medcineName, Integer medcineType, Double medcinePrice, Integer medcineCompany,Integer medcineBaoZhiQi,String medcineNote,String medcineEffect,String medcineImage) {
		MedcineDetail detail = new MedcineDetail();
		detail.setMedcineBaozhiqi(BigDecimal.valueOf(medcineBaoZhiQi.longValue()));
		detail.setMedcineEffect(medcineEffect);
		detail.setMedcineNote(medcineNote);
		detail.setMedcineImage(medcineImage);
		detail.setMedcineName(medcineName);
		detail.setMedcinePrice(BigDecimal.valueOf(medcinePrice.longValue()));
		detail.setMedcineCompany(medcineTypeService.getMedcineCompanyById(medcineCompany.longValue()));
		detail.setMedcineType(medcineTypeService.getMedcineTypeById(medcineType.longValue()));
		medcineTypeService.addMedcineDetail(detail);
	}
}
